// pages/card/card.js
// 用于展示不同卡卡片的详细数据,比如背景图,比如邮卡名称,比如title
import { api } from "../../utils/api.js";
let app = getApp();
let _detailData = [{
  "card_type_id": 1,
  "bg_pic_url": "http://zhao810.gitee.io/e-images/home/learnBorrow.png",
  "card_type_name": "开心邮",
  "title": "我是开心邮"
},
{
  "card_type_id": 2,
  "bg_pic_url": "http://zhao810.gitee.io/e-images/home/homeLoan.png",
  "card_type_name": "欢乐邮",
  "title": "我是欢乐邮"
},
{
  "card_type_id": 3,
  "bg_pic_url": "http://zhao810.gitee.io/e-images/home/payBorrow.png",
  "card_type_name": "畅快邮",
  "title": "我是畅快邮"
},
{
  "card_type_id": 4,
  "bg_pic_url": "http://zhao810.gitee.io/e-images/home/manager.png",
  "card_type_name": "悦享邮",
  "title": "我是悦享邮"
},
{
  "card_type_id": 5,
  "bg_pic_url": "http://zhao810.gitee.io/e-images/home/huiAPP.png",
  "card_type_name": "惠民邮",
  "title": "我是惠民邮"
}];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailData:null,
    shareLink:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //option接收卡片类型，让页面渲染指定内容的数据
    // console.log(options.card_type_id)
    let detailData = _detailData.filter(item=>{
      return item.card_type_id == options.card_type_id
      // return item.card_type_id == "5"//假装抽中2，方便测试
    })
    this.setData({
      detailData:detailData[0],
    })
    // onload获取卡片分享链接
    wx.request({
      url: api.getShareLink,
      data: {
        access_token: app.globalData.access_token,
        card_type_id: options.card_type_id
      },
      success: (result) => {
        // console.log("获取分享key：", result.data.data.share_link);
        let share_link = result.data.data.share_link.replace("http","https")
        this.setData({
          shareLink: share_link
        })
      }
    })
  },
  toMyCard(){
    wx.navigateTo({
      url: `/pages/myCard/myCard`,
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (opt) {
      
      if(opt.from === "button"){
        return {
          title: `送你一张${this.data.detailData.card_type_name}，一夜变油管大神！`,
          path:`/pages/index/index?getCardLink=${this.data.shareLink}`,
          imageUrl:`https://i.loli.net/2020/02/12/QbxMtDg6JE9qACV.png`
        }
      }else{
        return {
          title: "快来参与扫邮活动，你也有机会中大奖！",
          path: `/pages/index/index`,
          imageUrl: `https://i.loli.net/2020/02/12/QbxMtDg6JE9qACV.png`
        }
      }
  }
})